<?php
    /**
     *  
     */
    class ConsumerOrder extends BaseModel
    {
        CONST STATUS_ALL        = -1;
        CONST STATUS_OPEN       = 1;
        CONST STATUS_CLOSE      = 2;
        CONST STATUS_DELETE     = 9;

        protected $_id;
        protected $_orderKey;
        protected $_status;
        protected $_payMoney;
        protected $_payCard;
        protected $_photoQuantity;
        protected $_photoQuantityAppend;
        protected $_orderMoney;
        protected $_orderMoneyAppend;
        protected $_orderDate;
        protected $_createDate;
        protected $_updateDate;
        protected $_properties;

        /**
         *  讀取資料表而產生, 請視同cache, 任何情況下皆不可更動該產生的內容
         *  @return Array()
         */
        public static function getTableDefinition()
        {
            return Array();
        }

        /**
         *  經資料表傳至程式中時所對應的 method 資訊
         *  @return Array()
         */
        public static function getTableFields()
        {
            return Array(
                "id"                    => "getId",
                "order_key"             => "getOrderKey",
                "status"                => "getStatus",
                "pay_money"             => "getPayMoney",
                "pay_card"              => "getPayCard",
                "photo_quantity"        => "getPhotoQuantity",
                "photo_quantity_append" => "getPhotoQuantityAppend",
                "order_money"           => "getOrderMoney",
                "order_money_append"    => "getOrderMoneyAppend",
                "order_date"            => "getOrderDateByDb",
                "create_date"           => "getCreateDateByDb",
                "update_date"           => "getUpdateDateByDb",
                "properties"            => "getProperties"
            );
        }

        /**
         *  reset to default value model
         *  @return Array()
         */
        public function resetValue() {
            $this->__sleep();
            $this->setId                   ( -1 );
            $this->setOrderKey             ( NULL );
            $this->setStatus               ( ConsumerOrder::STATUS_OPEN );
            $this->setPayMoney             ( 0 );
            $this->setPayCard              ( 0 );
            $this->setPhotoQuantity        ( 0 );
            $this->setPhotoQuantityAppend  ( 0 );
            $this->setOrderMoney           ( 0 );
            $this->setOrderMoneyAppend     ( 0 );
            $this->setOrderDate            ( time() );
            $this->setCreateDate           ( time() );
            $this->setUpdateDate           ( time() );
            $this->setProperties           ( Array() );
        }

        /**
         *  validate model data
         *  @return messages Array()
         */
        public function validate()
        {
            $validateInfo = parent::validate();

            // choose value
            $result = false;
            foreach( $this->getStatusList('status') as $name => $id ) {
                if( $this->getStatus()==$id ) {
                    $result = true;
                    break;
                }
            }
            if(!$result) {
                $validateInfo['status']['message'] = 'status 不正確';
            }

            // timestamp
            if( $this->getCreateDate() < -28800 ) {
                $validateInfo['createDate']['message'] = '日期不正確';
            }
            
            // timestamp
            if( $this->getUpdateDate() < -28800 ) {
                $validateInfo['updateDate']['message'] = '日期不正確';
            }

            if( !$this->getOrderKey() ) {
                $validateInfo['orderKey']['message'] = '訂單編號必填';
            }

            return $validateInfo;
        }

        /**
         *  filter model data
         */
        public function filter()
        {
            $this->setOrderKey             ( trim(strip_tags( $this->getOrderKey() )) );
            $this->setStatus               ( (int) $this->getStatus() );
            $this->setPayMoney             ( (int) $this->getPayMoney() );
            $this->setPayCard              ( (int) $this->getPayCard() );
            $this->setPhotoQuantity        ( (int) $this->getPhotoQuantity() );
            $this->setPhotoQuantityAppend  ( (int) $this->getPhotoQuantityAppend() );
            $this->setOrderMoney           ( (int) $this->getOrderMoney() );
            $this->setOrderMoneyAppend     ( (int) $this->getOrderMoneyAppend() );
          //$this->setOrderDate            ();
          //$this->setCreateDate           ();
          //$this->setUpdateDate           ();
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            basic method 
        ------------------------------------------------------------------------------------------------------------------------ */


        /**
         *  get OrderKey
         *  @return string
         */
        public function getOrderKey()
        {
            return $this->_orderKey;
        }
        /**
         *  set OrderKey
         */
        public function setOrderKey( $orderKey )
        {
            $this->_orderKey = trim($orderKey);
        }

        /**
         *  get Status 
         *  @return int
         */
        public function getStatus()
        {
            return $this->_status;
        }
        /**
         *  set Status
         */
        public function setStatus( $status )
        {
            $this->_status = (int) $status;
        }

        /**
         *  get PayMoney 
         *  @return int
         */
        public function getPayMoney()
        {
            return $this->_payMoney;
        }
        /**
         *  set PayMoney
         */
        public function setPayMoney( $payMoney )
        {
            $this->_payMoney = (int) $payMoney;
        }

        /**
         *  get PayCard 
         *  @return int
         */
        public function getPayCard()
        {
            return $this->_payCard;
        }
        /**
         *  set PayCard
         */
        public function setPayCard( $payCard )
        {
            $this->_payCard = (int) $payCard;
        }

        /**
         *  get PhotoQuantity 
         *  @return int
         */
        public function getPhotoQuantity()
        {
            return $this->_photoQuantity;
        }
        /**
         *  set PhotoQuantity
         */
        public function setPhotoQuantity( $photoQuantity )
        {
            $this->_photoQuantity = (int) $photoQuantity;
        }

        /**
         *  get PhotoQuantityAppend 
         *  @return int
         */
        public function getPhotoQuantityAppend()
        {
            return $this->_photoQuantityAppend;
        }
        /**
         *  set PhotoQuantityAppend
         */
        public function setPhotoQuantityAppend( $photoQuantityAppend )
        {
            $this->_photoQuantityAppend = (int) $photoQuantityAppend;
        }

        /**
         *  get OrderMoney 
         *  @return int
         */
        public function getOrderMoney()
        {
            return $this->_orderMoney;
        }
        /**
         *  set OrderMoney
         */
        public function setOrderMoney( $orderMoney )
        {
            $this->_orderMoney = (int) $orderMoney;
        }

        /**
         *  get OrderMoneyAppend 
         *  @return int
         */
        public function getOrderMoneyAppend()
        {
            return $this->_orderMoneyAppend;
        }
        /**
         *  set OrderMoneyAppend
         */
        public function setOrderMoneyAppend( $orderMoneyAppend )
        {
            $this->_orderMoneyAppend = (int) $orderMoneyAppend;
        }

        /**
         *  set orderDate
         *  @param int , date int
         */
        function setOrderDate($int) 
        {
            $this->_orderDate = (int) $int;
        }
        /**
         *  get orderDate
         *  @return int;
         */
        function getOrderDate() 
        {
            if( !$this->_orderDate ) {
                $this->_orderDate = time();
            }
            return $this->_orderDate;
        }
        /**
         *  get orderDate by format 
         *  @param  format , date format string
         *  @return string
         */
        function getOrderDateByFormat( $format="Y-m-d" )
        {
            return date($format,$this->getOrderDate());
        }
        /**
         *  convert object-value to database 
         *  依照資料表欄位的格式不同, 謂給所需的格式
         *  @return string
         */
        function getOrderDateByDb()
        {
            // "datetime"
            return date("Y-m-d H:i:s",$this->getOrderDate());
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            extends
        ------------------------------------------------------------------------------------------------------------------------ */
        /**
         *  clear extends information
         */
        public function __sleep()
        {
            return parent::__sleep();
        }

    }

